const { host } = require('../../config.js');
// pages/picture/picture.js
var urlPath = require('../../config.js');

Page({
	// 前往下一页
	picture() {
		wx.navigateTo({
			url: '../picture/picture'
		});
	},
	again(e) {
		wx.navigateTo({
			url: '../sign/sign'
		});
	},

	/**
	 * 页面的初始数据
	 */
	data: {
		host: urlPath.host,
		AllUserMedal: [],
		id:"",
		img_url: wx.getStorageSync('img_url'),
		autoplay: false,
		interval: 3000,
		duration: 800,
		//数据
		name: wx.getStorageSync('user').name,
		nm_number: "0",
		badge_list: [{
			type: '1',
			url: '../../img/nav/zhonghe.png',
			urlIndex: '../../img/nav/zhongheIndex.png',
		}, {
			type: '2',
			url: '../../img/nav/jianpai.png',
			urlIndex: '../../img/nav/jianpaiIndex.png',
		}],
		//列表
		works_list: [{
				title: '我的证书',
				url: '/pages/certificate/certificate'
			},
			{
				title: '我的项目',
				url: ''
			},
			{
				title: '我的碳足迹',
				url: '/pages/footprint/footprint'
			}
		],
		userInfo: {
			// nickname: wx.getStorageSync('nickname'),
			// address: wx.getStorageSync('address'),
			// wx_avatar: wx.getStorageSync('wx_avatar'),
		},
		usesDetails: {}
	},
	gocertificate_detail: function (e) {
		wx.navigateTo({
			url: e.currentTarget.dataset.url
		});
	},
	goUser: function (e) {

		wx.navigateTo({
			url:'/pages/userEdit/userEdit'
		});
	},
	getRoute: function (e) {

		wx.navigateTo({
			url: e.mark.url + '?id=1'
		});
	},
	upUserInfo:function(){
		wx.request({
			url: urlPath.apiEditUserInfo,
			method: 'post',
			header: {
				'content-type': 'application/x-www-form-urlencoded',
				'openid': wx.getStorageSync('openid')
			},
			dataType: 'json', // 添加这个配置
			data: {
				'openid': wx.getStorageSync('openid'),
				nick_name: this.data.userInfo.nickName,
				province: this.data.userInfo.province ,
				city: this.data.userInfo.city,
				county: '',
				wx_avatar:this.data.userInfo.avatarUrl
			},
			success(res) {
	
			}
		})
	},
	// /获取用户信息 编辑
	getUserInfo: function () {
		var that = this
		wx.request({
			url: urlPath.getUserInfo,
			header: {
				'content-type': 'application/x-www-form-urlencoded'
			},
			dataType: 'json', // 添加这个配置
			method: 'post',
			data: {
				openid: wx.getStorageSync('openid')
			},
			success(res) {
				if (res.data.result == 'success') {
					var data = JSON.parse(res.data.data)
					// console.log(data,'ddd',getApp().globalData.userInfo)
					var url = data.user.avatar != '' ? host+(data.user.avatar) : ''
					let userInfo={
						avatarUrl:url,
						nickName:data.user.nickname,
						area_str:data.user.area_str,
						province:data.user.province,
						city:data.user.city,
						county:data.user.county
					}
					// that.onAuth()
					that.setData({
						userInfo,
						usesDetails: data
					})
					// console.log(data, that.data.usesDetails, 'data')
				} else {
					console.log(res.data.msg); //失败
				}
			}
		});
	},
	onAuth() {
		let that = this
		wx.getUserProfile({
			desc: '用于完善会员资料',
			lang: "zh_CN",
			success: function (res) {
				getApp().globalData.userInfo = res.userInfo
				let {avatarUrl,province,city,country,nickName} = res.userInfo
				let area_str = province+'•'+city
				that.upUserInfo()
				that.setData({
					userInfo:{
						avatarUrl:avatarUrl,
						nickName:nickName,
						area_str:area_str,
						province:province,
						city:city,
						county:'',
					},
				})
				
			}
		})
	},
	getUser: function (e) {

	},

	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function (options) {},

	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady: function () {

	},



	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide: function () {

	},

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload: function () {

	},

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh: function () {

	},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom: function () {

	},

	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage: function () {

	},
	onShow: function () {
		if (typeof this.getTabBar === 'function' &&
			this.getTabBar()) {
			this.getTabBar().setData({
				selected: 1
			})
		}

		var that = this;
		that.getUserInfo();
		wx.request({
			url: urlPath.apiAllUserMedal,
			header: {
				'content-type': 'application/x-www-form-urlencoded'
			},
			dataType: 'json', // 添加这个配置
			method: 'post',
			data: {
				openid: wx.getStorageSync('openid')
			},
			success(res) {
				if (res.data.result == 'success') {
					// console.log(JSON.parse(res.data.data))
					that.setData({
						AllUserMedal: JSON.parse(res.data.data)
					})
				} else {
					console.log(res.data.msg); //失败
				}
			}
		});
	},
	getImgs: function () {
		var that = this;
		wx.request({
			url: urlPath.getImgs,
			header: {
				'token': wx.getStorageSync('token')
			},
			success(res) {
				if (res.data.code == 0) {
					if (res.data.data.length > 0) {
						var works_list = [];
						var num = 0;
						res.data.data.forEach(function (item, index) {
							works_list.push({
								ids: item.id,
								img: urlPath.host + item.pic,
								text: item.explain,
								number: item.votes,
							});
							num = num + item.votes;
						});
						that.setData({
							works_list: works_list,
							nm_number: num
						});
					}
				} else {
					console.log(res); //失败
				}
			}
		});
	}
})